package zjc.bookkeeping.fragment;

import static zjc.bookkeeping.R.drawable.rectangle;
import static zjc.bookkeeping.R.drawable.rectangle_selected;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import zjc.bookkeeping.R;
import zjc.bookkeeping.fragment.statistics.StatisticsMonthFragment;
import zjc.bookkeeping.fragment.statistics.StatisticsWeekFragment;
import zjc.bookkeeping.fragment.statistics.StatisticsYearFragment;
import zjc.bookkeeping.minepage.dAccountbookmanagement.AccountbookmanagementMainActivity;

public class StatisticsFragment extends Fragment {
    private View fragment_statistics;
    private TextView weekView;
    private TextView monthView;
    private TextView yearView;
    private Button btn_ledger_switch;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    // 绑定控件
    public void findView(){
        weekView = fragment_statistics.findViewById(R.id.week_view);
        monthView = fragment_statistics.findViewById(R.id.month_view);
        yearView = fragment_statistics.findViewById(R.id.year_view);
        btn_ledger_switch = fragment_statistics.findViewById(R.id.ledger_switch_button);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findView();//绑定控件
        init();//初始化界面默认为周统计
        setListener();//监听
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragment_statistics = inflater.inflate(R.layout.fragment_statistics,container,false);
        findView();
        return fragment_statistics;
    }

    // 绑定监听器
    public void setListener(){
        Log.i("1","监听开启");
        btn_ledger_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AccountbookmanagementMainActivity.class);
                startActivity(intent);
//                FragmentManager fragmentManager = getFragmentManager();
//                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//                fragmentTransaction.replace(, new LedgerSwitchFragment());
//                fragmentTransaction.commit();
            }
        });
        weekView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

                // 更换标签颜色
                weekView.setTextColor(Color.WHITE);
                weekView.setBackgroundResource(rectangle_selected);
                monthView.setTextColor(0xff808080);
                monthView.setBackgroundResource(rectangle);
                yearView.setTextColor(0xff808080);
                yearView.setBackgroundResource(rectangle);
                // 切换碎片
                replaceCurrentPage(new StatisticsWeekFragment());
                Log.i("1","week");
            }
        });

        monthView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                // 更换标签颜色
                monthView.setTextColor(Color.WHITE);
                monthView.setBackgroundResource(rectangle_selected);
                weekView.setTextColor(0xff808080);
                weekView.setBackgroundResource(rectangle);
                yearView.setTextColor(0xff808080);
                yearView.setBackgroundResource(rectangle);
                // 切换碎片
                replaceCurrentPage(new StatisticsMonthFragment());
                Log.i("1","month");
            }
        });

        yearView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                // 更换标签颜色
                yearView.setTextColor(Color.WHITE);
                yearView.setBackgroundResource(rectangle_selected);
                weekView.setTextColor(0xff808080);
                weekView.setBackgroundResource(rectangle);
                monthView.setTextColor(0xff808080);
                monthView.setBackgroundResource(rectangle);
                // 切换碎片
                replaceCurrentPage(new StatisticsYearFragment());
                Log.i("1","year");
            }
        });
    }

    // 切换当前页面
    public void replaceCurrentPage(Fragment fragment){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl_blank, fragment);
        fragmentTransaction.commit();
    }

    // 初始化界面，默认显示周统计
    public void init(){
        replaceCurrentPage(new StatisticsWeekFragment());
        weekView.setTextColor(Color.WHITE);
    }

}